package com.abbas.wallerpaper.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.abbas.wallerpaper.R;
import com.abbas.wallerpaper.base.BaseActivity;
import com.google.android.material.checkbox.MaterialCheckBox;
import com.google.android.material.progressindicator.LinearProgressIndicator;

public class SplashActivity extends BaseActivity {

    private ImageView imAppIcon;
    private TextView tvAppName;
    private LinearLayout lvInit;
    private TextView tvInitTip;
    private ProgressBar pi;
    private LinearLayout lvStart;
    private Button btStart;
    private CheckBox cb;
    private TextView tvProtect;

    private final int LOADING_SUCCESS = 0x2471;
    private final int LOADING_FAILURE = 0x3487;

    @SuppressLint("HandlerLeak")
    private final Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case LOADING_SUCCESS:
                    pi.setProgress(pi.getProgress() + 10);
//                    pi.setProgressCompat(pi.getProgress()+10,false);
                    tvInitTip.setText("Initialing... " + pi.getProgress() + "%");
                    if (pi.getProgress() == 100) {
                        lvInit.setVisibility(View.GONE);
                        lvStart.setVisibility(View.VISIBLE);
                        Animation animation = AnimationUtils.loadAnimation(SplashActivity.this, R.anim.in_ani);
                        lvStart.setAnimation(animation);
                        break;
                    }
                    handler.sendEmptyMessageDelayed(LOADING_SUCCESS, 200);
                    break;
                case LOADING_FAILURE:
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void init() {
        imAppIcon = (ImageView) findViewById(R.id.im_app_icon);
        tvAppName = (TextView) findViewById(R.id.tv_appName);
        lvInit = (LinearLayout) findViewById(R.id.lv_init);
        tvInitTip = (TextView) findViewById(R.id.tv_initTip);
        pi = findViewById(R.id.pi);
        lvStart = (LinearLayout) findViewById(R.id.lv_start);
        btStart = (Button) findViewById(R.id.bt_start);
        cb = findViewById(R.id.cb);
        tvProtect = (TextView) findViewById(R.id.tv_protect);

        SpannableStringBuilder builder = new SpannableStringBuilder("Agree to Privacy policy and Terms of Use");
        //这个一定要记得设置，不然点击不生效
        tvProtect.setMovementMethod(LinkMovementMethod.getInstance());
        builder.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                showTip(widget, "隐私协议");
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(getResources().getColor(R.color.yellow_basic));
            }
        }, 10 - 1, 23, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        builder.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                showTip(widget, "用户协议");
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(getResources().getColor(R.color.yellow_basic));
            }
        }, 29 - 1, 40, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tvProtect.setText(builder);

//        btStart.setTypeface(getResources().getFont(R.font.Arial));
    }

    @Override
    protected void initEvent() {

        btStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SplashActivity.this, MainActivity.class));
            }
        });


        handler.sendEmptyMessageDelayed(LOADING_SUCCESS, 1000);

    }
}